Imports CAPA_Utilitarios
Imports System.Data.SqlClient
Imports CAPA_Entidad
Public Class DocumentoClienteDatos

    'Se crea una instancia unica para las conexiones
    Dim conex As Conexion = Conexion.getInstance()

    Function obtenerDocumentoCliente(ByVal ciaCodigo As String, ByVal sucCodigo As String, ByVal tdocCodigo As String, ByVal codDCI As String) As DocumentoCliente
        Dim documento As DocumentoCliente = Nothing
        Dim coneccion As SqlConnection = conex.obtenerConexion()
        Try
            coneccion.Open()
            Dim query As String = "SELECT * FROM VW_DocumentoCliente " & _
                                  "WHERE CIA = @CIA AND SUC_Codigo = @SUC AND TDOC_Codigo = @TDOC AND DCI_Codigo = @DCI"

            Dim comando As New SqlCommand(query, coneccion)
            With comando
                .Parameters.AddWithValue("@CIA", ciaCodigo)
                .Parameters.AddWithValue("@SUC", sucCodigo)
                .Parameters.AddWithValue("@TDOC", tdocCodigo)
                .Parameters.AddWithValue("@DCI", codDCI)
            End With
            Dim adapter As New SqlDataAdapter(comando)
            Dim ds As New DataSet
            adapter.Fill(ds)

            If ds.Tables(0).Rows.Count > 0 Then
                documento = New DocumentoCliente(ds.Tables(0).Rows(0)("CIA").ToString.Trim, ds.Tables(0).Rows(0)("SUC_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("TDOC_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DCI_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("CLI_Codigo").ToString.Trim, _
                                    ds.Tables(0).Rows(0)("VEN_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("MON_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("TDO_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("CNV_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("FOP_Codigo").ToString.Trim, _
                                    ds.Tables(0).Rows(0)("AGE_Codigo").ToString.Trim, CDec(ds.Tables(0).Rows(0)("DCI_TotalBruto").ToString.Trim), CDec(ds.Tables(0).Rows(0)("DCI_TotalDsct").ToString.Trim), CDec(ds.Tables(0).Rows(0)("DCI_TotalNeto").ToString.Trim), _
                                    CDec(ds.Tables(0).Rows(0)("DCI_IGV").ToString.Trim), CDec(ds.Tables(0).Rows(0)("DCI_Total").ToString.Trim), ds.Tables(0).Rows(0)("DCI_Observaciones").ToString.Trim, ds.Tables(0).Rows(0)("DCI_ObsCredito").ToString.Trim, _
                                    ds.Tables(0).Rows(0)("DCI_DireccionEntrega").ToString.Trim, ds.Tables(0).Rows(0)("DCI_Estado").ToString.Trim, ds.Tables(0).Rows(0)("CLEN_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DCI_FechaEntrega").ToString.Trim, _
                                    ds.Tables(0).Rows(0)("DCI_Oficina").ToString.Trim, ds.Tables(0).Rows(0)("DCI_Muestra").ToString.Trim, ds.Tables(0).Rows(0)("DCI_ConfPersona").ToString.Trim, ds.Tables(0).Rows(0)("DCI_RecPersona").ToString.Trim, _
                                    ds.Tables(0).Rows(0)("ATS_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("BAN_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DCI_NDeposito").ToString.Trim, ds.Tables(0).Rows(0)("DCI_RazonCot").ToString.Trim, ds.Tables(0).Rows(0)("Fecha").ToString.Trim, Nothing)
            Else
                documento = New DocumentoCliente("-1", ds.Tables(0).Rows(0)("SUC_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("TDOC_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DCI_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("CLI_Codigo").ToString.Trim, _
                                    ds.Tables(0).Rows(0)("VEN_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("MON_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("TDO_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("CNV_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("FOP_Codigo").ToString.Trim, _
                                    ds.Tables(0).Rows(0)("AGE_Codigo").ToString.Trim, CDec(ds.Tables(0).Rows(0)("DCI_TotalBruto").ToString.Trim), CDec(ds.Tables(0).Rows(0)("DCI_TotalDsct").ToString.Trim), CDec(ds.Tables(0).Rows(0)("DCI_TotalNeto").ToString.Trim), _
                                    CDec(ds.Tables(0).Rows(0)("DCI_IGV").ToString.Trim), CDec(ds.Tables(0).Rows(0)("DCI_Total").ToString.Trim), ds.Tables(0).Rows(0)("DCI_Observaciones").ToString.Trim, ds.Tables(0).Rows(0)("DCI_ObsCredito").ToString.Trim, _
                                    ds.Tables(0).Rows(0)("DCI_DireccionEntrega").ToString.Trim, ds.Tables(0).Rows(0)("DCI_Estado").ToString.Trim, ds.Tables(0).Rows(0)("CLEN_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DCI_FechaEntrega").ToString.Trim, _
                                    ds.Tables(0).Rows(0)("DCI_Oficina").ToString.Trim, ds.Tables(0).Rows(0)("DCI_Muestra").ToString.Trim, ds.Tables(0).Rows(0)("DCI_ConfPersona").ToString.Trim, ds.Tables(0).Rows(0)("DCI_RecPersona").ToString.Trim, _
                                    ds.Tables(0).Rows(0)("ATS_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("BAN_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DCI_NDeposito").ToString.Trim, ds.Tables(0).Rows(0)("DCI_RazonCot").ToString.Trim, ds.Tables(0).Rows(0)("Fecha").ToString.Trim, Nothing)
            End If
        Catch ex As Exception
            documento = Nothing
        Finally
            coneccion.Close()
        End Try

        Return documento
    End Function

End Class
